<script setup lang="ts">
import {useRouter} from "vue-router";
import TeamCardList from "../components/TeamCardList.vue";
import {onMounted, ref} from "vue";
import myAxios from "../plugins/MyAxios.ts";
import {Toast} from "vant";

const teamList = ref([])
const router = useRouter();
const active = ref('public')
//是否加载过
let isLoad = ref(false);
//跳转到添加队伍页
const toAddTeam = () => {
  router.push({
    path: "/team/add"
  });
}
/**
 * 切换查询状态
 * @param name
 */
const onTabChange = (name) => {
  // 查公开
  if (name === 'public') {
    listTeam(searchText.value, 0);
  } else {
    // 查加密
    listTeam(searchText.value, 2);
  }
}
const listTeam = async (val = '', status) => {
  const res = await myAxios.get("/team/list", {
    params: {
      searchText: val,
      pageNum: 1,
      status,
    }
  });
  //@ts-ignore
  if (res?.code === 0) {
    teamList.value = res.data;
  } else {
    Toast.fail("加载队伍失败，请刷新重试")
  }
  if (!isLoad) {
    location.reload()
    isLoad.value = true;
  }
}
const searchText = ref('')
const onSearch = (val) => {
  listTeam(val);
};
//页面加载时只会触发一次
onMounted(async () => {
  listTeam();
})
</script>

<template>
  <div id="TeamPage">
    <van-search v-model="searchText" placeholder="搜索队伍" @search="onSearch"/>
    <van-tabs v-model:active="active" @change="onTabChange">
      <van-tab title="公开" name="public"/>
      <van-tab title="加密" name="private"/>
    </van-tabs>
    <div style="margin-bottom: 16px"/>
    <van-button class="add-button" icon="plus" type="primary" @click="toAddTeam"/>
    <team-card-list :team-list="teamList"/>
    <van-empty v-if="teamList?.length<1" description="数据为空"/>
  </div>
</template>

<style scoped>

</style>